﻿using System;
using System.Reflection;

namespace Byond.UIRuntime
{
	class PropertyRegistration
	{
		protected Action<IUiControl, Action<object>> _setupBindingCallback;

		public PropertyRegistration(PropertyInfo propertyInfo)
		{
			PropertyInfo = propertyInfo;
		}

		public PropertyInfo PropertyInfo { get; private set; }
		public bool IsActionBinding { get { return _setupBindingCallback != null; } }

		public void RegisterBinding(IUiControl instance, Action<object> action)
		{
			if (!IsActionBinding)
				throw new Exception("Cannot register a binding on a non action property.");
			
			_setupBindingCallback(instance, action);
		}
	}

	internal class PropertyRegistration<T> : PropertyRegistration
	{
		public PropertyRegistration(PropertyInfo propertyInfo)
			: base(propertyInfo)
		{
		}

		// TODO: Smoothing hier reinziehen; nicht "global" für einen Control-Typ machen

		public PropertyRegistration<T> BindAsAction(Action<T, Action<object>> setupBindingCallback)
		{
			_setupBindingCallback = (instance, callback)
				=> setupBindingCallback((T)instance, callback);
			
			return this;
		}
	}
}